<?php
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Version;

/**
 * 后台-版本控制--crud
 * 
 *        
 */
class VersionController extends AdminController
{
    private $_attr = array(
        'version',
        'name',
        'force',
        'updateurl',
        'type'
    );
    
    // 添加
    public function add(Request $request)
    {
        $newAddress = new Version(); 
        foreach ($this->_attr as $attr) {
            $newAddress->$attr = trim($request->input($attr, ''));
        }
        if ($newAddress->save()) {
            $this->output($newAddress->toArray());
        } else {
            $this->err('保存错误');
        }
    }

    // 更新
    public function update(Request $request)
    {
        $id = trim($request->input('id'));
        $newAddress = Version::where('id', $id)->first(); 
        foreach ($this->_attr as $attr) {
            $newValue = trim($request->input($attr, ''));
            if ($newValue) {
                $newAddress->$attr = $newValue;
            }
        }
        if ($newAddress->save()) {
            $this->output($newAddress->toArray());
        } else {
            $this->err('保存错误');
        }
    }

    // 删除
    public function del(Request $request)
    {
        $id = trim($request->input('id'));
        $newAddress = Version::where('id', $id)->first();
        if ($newAddress->delete()) {
            $this->output('删除成功');
        } else {
            $this->err('删除错误');
        }
    }

    // 列表
    public function alist(Request $request)
    {
        $page = trim($request->input('page', 0)); // 页码
        $pagenumber = trim($request->input('pagenumber', '30')); // 分页个数
        $fliter = Version::where('id', '>', '0')->orderBy('id', 'desc');
        $columns= [
            'id','version','type','name', 'force', 'updateurl', 'created_at', 'updated_at'
        ];
        $data = $fliter->paginate($pagenumber, $columns, 'page', $page + 1);
        $this->output(array(
            "count" => $data->total(),
            "items" => $data->items(),
            "page" => $page
        ));
        
    }
}